package org.example.controller;

import org.example.model.table.ClassTable;
import org.example.model.vo.ClassVo;
import org.example.service.ClassTableService;
import org.example.util.CommonRequest;
import org.example.util.CommonRequestHandle;
import org.example.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Classname ClassController
 * @Description
 * @Version 1.0.0
 * @Date 2022/12/13 11:37
 * @Created by Enzuo
 */

@RestController
@RequestMapping("/class")
@CrossOrigin(origins = "*",maxAge = 3600)
public class ClassController {
    @Resource
    ClassTableService classTableService;
    @GetMapping("/classInfo")
    public Result getClassInfoById(
            @RequestParam(required = false) Long id,
            @RequestParam(defaultValue = "1",required = false) int page,
            @RequestParam(defaultValue ="10",required = false) int size
    ){
        if (id==null)return Result.error().data("msg","请携带班级id").setCode(403);
        if(size > 500) size =500;

        return classTableService.getClassInfoById(id, page, size);
    }

    @GetMapping("/getClassInfo")
    public Result getAllClassInfo(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue ="10") int size
    ){
        if(size > 500) size =500;

        return classTableService.getAllClassInfo(page, size);
    }

    @DeleteMapping("/deleteClass")
    public Result deleteClass(@RequestParam(required = false) Long id){
        if (id==null)return Result.error().data("msg","请携带班级id").setCode(403);

        return classTableService.deleteClassById(id);
    }

    @PostMapping("/addClass")
    public Result addClass(@RequestBody CommonRequest commonRequest){
        ClassTable classTable = CommonRequestHandle.getRequest(commonRequest, ClassTable.class);
        return classTableService.addClass(classTable);
    }

    @PostMapping("/changeClass")
    public Result changeClass(@RequestBody CommonRequest commonRequest){
        ClassTable request = CommonRequestHandle.getRequest(commonRequest, ClassTable.class);
        return classTableService.changeClass(request);
    }

    @GetMapping("/getClassAll")
    public Result getClassAll(){
        return classTableService.getClassList();
    }

    @GetMapping("/getClassPage")
    public Result getClassPage(
            @RequestParam(defaultValue = "1",required = false) Integer page,
            @RequestParam(defaultValue ="10",required = false, name = "limit") Integer size,
            ClassTable classTable
    ){
        return classTableService.getClassPage(page, size, classTable);
    }
}
